package com.bawei.module_video_hall.mvi.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.lib_mvi.base.BaseViewModel
import com.bawei.module_video_hall.mvi.intent.VideoHallIntent
import com.bawei.module_video_hall.mvi.repository.VideoHallRepository
import com.bawei.module_video_hall.mvi.state.VideoHallState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch

/**
 *@author：lxy
 *@decription:
 *@time：2025/6/23
 */
class VideoHallViewModel : BaseViewModel<VideoHallIntent>() {

    private val repository: VideoHallRepository by lazy { VideoHallRepository() }

    private var _multiTypeFlow = MutableStateFlow<VideoHallState>(VideoHallState.Init)

    val multiTypeFlow = _multiTypeFlow.asStateFlow()

    private var _recommendVideoFlow = MutableStateFlow<VideoHallState>(VideoHallState.Init)

    val recommendVideoFlw = _recommendVideoFlow.asStateFlow()

    private var _VideoHallFlow = MutableStateFlow<VideoHallState>(VideoHallState.Init)

    val videoHallFlow = _VideoHallFlow.asStateFlow()


    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect {
                when (it) {
                    is VideoHallIntent.GetMultiType -> {
                        getMultiType()
                    }

                    is VideoHallIntent.GetRecommendVideo -> {
                        doRecommendVideos(it)
                    }

                    is VideoHallIntent.GetMultiVideoByTypeId -> {
                        getVideoByTypeId(it)
                    }
                }
            }
        }
    }

    private fun getVideoByTypeId(it: VideoHallIntent.GetMultiVideoByTypeId) {
        viewModelScope.launch {
            repository.getMultiVideoByTypeId(it.page, it.pageSize, it.typeId)
                .catch {
                    _VideoHallFlow.value = VideoHallState.Failed(it.message.toString())
                }
                .collect {
                    if (it.code == 200) {
                        if (it.data.isEmpty()) {
                            _VideoHallFlow.value = VideoHallState.Empty
                        } else {
                            _VideoHallFlow.value =
                                VideoHallState.VideoRecommendSuccess(it.data)
                        }
                    } else {
                        _VideoHallFlow.value = VideoHallState.Failed(it.msg)
                    }
                }
        }
    }

    /**
     * 获取推荐的视频信息
     */
    private fun doRecommendVideos(it: VideoHallIntent.GetRecommendVideo) {
        viewModelScope.launch(Dispatchers.IO) {
            repository.getRecommendVideo(it.page, it.pageSize)
                .catch {
                    _VideoHallFlow.value = VideoHallState.Failed(it.message.toString())
                }
                .collect {
                    if (it.code == 200) {
                        if (it.data.isEmpty()) {
                            _VideoHallFlow.value = VideoHallState.Empty
                        } else {
                            _VideoHallFlow.value =
                                VideoHallState.VideoRecommendSuccess(it.data)
                        }
                    } else {
                        _VideoHallFlow.value = VideoHallState.Failed(it.msg)
                    }
                }
        }
    }


    private fun getMultiType() {
        viewModelScope.launch {
            repository.getMultiType()
                .catch { e ->
                    _multiTypeFlow.value = VideoHallState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect {
                    if (it.code == 200) {
                        if (it.data.isEmpty()) {
                            _multiTypeFlow.value = VideoHallState.Empty
                        } else {
                            _multiTypeFlow.value = VideoHallState.VideoTypeSuccess(it.data)
                        }
                    } else {
                        _multiTypeFlow.value = VideoHallState.Failed(it.msg)
                    }
                }
        }
    }


}
